/*
 *  ui_dialog 0.1
 *  独门绝学出品
 *
 *	备份于 2011年  作者  董恒
 *
 *	备份日期:2011-11-03
 **/
(function($){
    //弹出框默认调用方法
    $.fn.ui_dialog = function(opts){
    	
    };
    
    $.ui_dialog_create = function(){
        return new ui_dialog_component();
    };
	
	//给每次创建的索引加1
	$.ui_dialog_zIndex = 1001;
	$.ui_dialog_id = Date.parse(new Date());
	
    //内部方法，创建弹出框
    function ui_dialog_component(){
		
    	if (typeof ui_dialog_component.baseParam == "undefined") {
        
            //初始化单选组件参数为必配置参数
            ui_dialog_component.baseParam = {
                width:700, 						//宽度
				height:270,						//高度
				title:''						//标题
            };
        };
    
        return {
            //所有属性方法配置详细列表
            settings: {
				shelter:false,					//是否遮挡
				dragging:false,					//标记拖曳状态
				isDrag:true,					//默认可以拖拽
				iX:false,						//原x轴
				iY:false,						//原y轴
				noClone:false,					//非克隆节点
				asId:false,						//对装过来的ID进行重新命名
				title:'',						//标题
				requestUrl:'',					//请求地址
				bindId:'',						//绑定ID
				postUrl:'',						//post请求
				width:700,						//弹出框宽度
				height:'auto',					//弹出框高度
				uiDialogExClass:'ui-dialog',	//扩展名
                callback: {
                    closeDialog: function(){
                    	
                    },
					openDialog:function(){
						
					},
					confirmDialog:function(){
						
					}
                },
				params:{						//外带参数集合
					
				}						
            },
            /**
             * 功能：创建一个新的弹出框
             * @参数 {Object} elem 菜单框对象
             */
            _create: function(conf){
            	var _this = this;
                var dialogId = $.ui_dialog_id++;
                //取得弹出框个性化配置
                var opts = $.extend({}, conf);
				//弹出框ID
				_this.uiDialogId = 'ui_dialog_'+dialogId;
				//弹出框遮罩层ID
				_this.uiDialogShelterId = 'ui_dialog_shelter_'+dialogId;
				
				//将弹出框的默认配置和个性配置合并
				if (opts && opts.callback) {
                    this.settings.callback = $.extend({}, this.settings.callback, opts.callback);
                    delete opts.callback;
                }
				if (opts && opts.shelter) {
                    this.settings.shelter = opts.shelter;
                    delete opts.shelter;
                }
				if (opts && opts.title) {
                    this.settings.title =opts.title;
                    delete opts.title;
                }
				if (opts && opts.bindId) {
                    this.settings.bindId = opts.bindId;
                    delete opts.bindId;
                }
				if (opts && opts.width) {
                    this.settings.width = opts.width;
                    delete opts.width;
                }
                if (opts && opts.height) {
                    this.settings.height = opts.height;
                    delete opts.height;
                }
				if (opts && opts.params) {
                    this.settings.params = opts.params;
                    delete opts.params;
                }
				if (opts && opts.noClone) {
                    this.settings.noClone = opts.noClone;
                    delete opts.noClone;
                }
				if (opts && opts.asId) {
                    this.settings.asId = opts.asId;
                    delete opts.asId;
                }
				if (opts && typeof(opts.isDrag)!='undefined') {
                    this.settings.isDrag = opts.isDrag;
                    delete opts.isDrag;
                }
				
				//查看是否增加遮罩层
				if(this.settings.shelter){
					var uiDialogShelter = $('<div></div>')
						.addClass('ui-dialog-shelter')
						.appendTo(window.document.body)
						.css({
							'zIndex': $.ui_dialog_zIndex++,
							'position':'absolute',
							'width':'100%',
							'height':'100%',
							'filter':'alpha(opacity=0.3)',
							'opacity':'0.3',
							'background-color':'#000000',
							'top':'0px',
							'left':'0px'
						})
						.attr('id',_this.uiDialogShelterId)
						.hide();
				}
				var settings = this.settings,
					title = settings.title || '友情提示',
					_callback = settings.callback,
					
					//弹出框主体
					uiDialog = (_this.uiDialog = $('<div></div>'))
						.appendTo(window.document.body)
						.addClass(settings.uiDialogExClass)
						.hide()
						.css({
							'zIndex':$.ui_dialog_zIndex++,
							'position':'absolute',
							'left':($(window).width()-_this.uiDialog.width()-2)/2,
							'top':($(window).height()-_this.uiDialog.height()-2)/2,
							'width':this.settings.width,
							'height':'auto'
						})
						.attr('id',_this.uiDialogId),
					//弹出框标题
					uiDialogTitle = (_this.uiDialogTitle = $('<div></div>'))
						.addClass(settings.uiDialogExClass+'-title')
						.text(this.settings.title)
						.appendTo(uiDialog),
					//弹出框关闭按钮
					uiDialogClose = (_this.uiDialogClose = $('<div></div>'))
						.addClass(settings.uiDialogExClass+'-close')
						.hover(function(){
							$(this).addClass('ui-dialog-close-mouseover');
						},function(){
							$(this).removeClass('ui-dialog-close-mouseover');
						}).click(function(){
							_this.closeDialog();	
						})
						.appendTo(uiDialogTitle),
					uiDialogContent = (_this.uiDialogContent = $('<div></div>'))
					    .height(settings.height)
						.addClass(settings.uiDialogExClass+'-content')
						.appendTo(uiDialog),
					//装入iframe以便装载地址
					uiDialogFrame = (_this.uiDialogFrame = $('<iframe></iframe>'))
						.css({
							width:'100%',
							'overflow-y':'auto'
						})
						.attr({
							'src':_this.settings.requestUrl?_this.settings.requestUrl:'',
							'id':'frame'+ dialogId
						})
						.appendTo(uiDialogContent),
					uiDialogOp = (_this.uiDialogOp = $('<div></div>'))
						.addClass(settings.uiDialogExClass+'-op')
						.appendTo(uiDialog),
					uiDialogCancel = (_this.uiDialogCancel = $('<input type="button"/>'))
						.hover(function(){
							$(this).addClass('ui-dialog-cancel-mouseover');
						},function(){
							$(this).removeClass('ui-dialog-cancel-mouseover');
						})
						.click(function(){
							_this.closeDialog(uiDialogContent);
						})
						.val('取 消')
						.addClass(settings.uiDialogExClass+'-cancel')
						.appendTo(uiDialogOp),
					uiDialogOk = (_this.uiDialogOk = $('<input type="button"/>'))
						.hover(function(){
							$(this).addClass('ui-dialog-ok-mouseover');
						},function(){
							$(this).removeClass('ui-dialog-ok-mouseover');
						})
						.click(function(){
							var result = _callback.confirmDialog(uiDialogContent);
							if(result==1)
								_this.closeDialog(uiDialogContent);
						})
						.val('确 定')
						.addClass(settings.uiDialogExClass+'-ok')
						.appendTo(uiDialogOp);
						
					//如果有绑定的就赋予绑定的id内容
					$(function(){
						var cloneObj;
						if(settings.noClone)
							cloneObj = $('#'+settings.bindId);
						else
							cloneObj = $('#'+settings.bindId).clone();
						if(settings.bindId){
							uiDialogContent.html(cloneObj);
							if(settings.asId)
								uiDialogContent.find('div').attr('id',settings.asId);
							uiDialogContent.find('div').show();
						}
					});			
					
					//装入必要的参数
					if(this.settings.params){
						for(var param in this.settings.params){
							uiDialogContent.attr(param+'',this.settings.params[param]);
						}
					}	
					if(this.settings.isDrag){
						_this.uiDialogStartDrag();
						_this.uiDialogMoveDrag();
						_this.uiDialogMoveRelease();
					}
					
            },
            state:{
                isOpen:false,
                isHide:false,
                isClose:false
            },
        	/**
        	 * 开启对话框
        	 */
			openDialog:function(){
				var _this = this;
				 
				$('#'+_this.uiDialogShelterId).css({
					'opacity':'0'
				}).show().animate({
					'opacity':'0.3'
				});   
				var _width = $('#'+_this.uiDialogId).width(),
				_height = $('#'+_this.uiDialogId).height(),
				_winWidth = $(window).width(),
				_winHeight = $(window).height();
				
				$('#'+_this.uiDialogId).css({
					width:_width,
					height:'auto',
					left:(_winWidth-_width-2)/2,
					top:(_winHeight-_height-2)/2,
					'opacity':'0'
				}).show();
				$('#'+_this.uiDialogId).animate({
					'opacity':'1'
				},400);
				this.state.isOpen = true;
				this.state.isHide = false;
				this.state.isClose = false;
				//打开前执行
				_this.settings.callback.openDialog(_this.uiDialogContent);
			},
			showDialog:function(){
			    if(this.settings.callback.showDialog)
                    this.settings.callback.showDialog(this.uiDialogContent);
                 
                $('#'+this.uiDialogShelterId).css({
                    'opacity':'0'
                }).show().animate({
                    'opacity':'0.3'
                });   
                var width = $('#'+this.uiDialogId).width(),
                height = $('#'+this.uiDialogId).height(),
                winWidth = $(window).width(),
                winHeight = $(window).height();
                
                $('#'+this.uiDialogId).css({
                    width:width,
                    height:'auto',
                    left:(winWidth-width-2)/2,
                    top:(winHeight-height-2)/2,
                    'opacity':'0'
                }).show();
                $('#'+this.uiDialogId).animate({
                    'opacity':'1'
                },400);
                this.state.isOpen = true;
                this.state.isClose = false;
                this.state.isHide = false;
			},
			/**
			 * 关闭对话框
			 */
			closeDialog:function(){
			    if(this.settings.callback.closeDialog)
				    this.settings.callback.closeDialog(this.uiDialogContent);
				$('#'+this.uiDialogShelterId).hide();
				$('#'+this.uiDialogId).hide();
				this.state.isOpen = false;
				this.state.isClose = true;
				this.state.isHide = false;
			},
			/**
             * 关闭对话框
             */
            hideDialog:function(){
                $('#'+this.uiDialogShelterId).hide();
                $('#'+this.uiDialogId).hide();
                this.state.isOpen = false;
                this.state.isClose = false;
                this.state.isHide = true;
            },
			/**
             * 删除对话框 
             */
            removeDialog:function(){
                this.state.isOpen = false;
                this.state.isClose = false;
                this.state.isHide = false;
				var _this = this;
				$('#'+_this.uiDialogShelterId).remove();
				$('#'+_this.uiDialogId).remove();
            },
			/**
			* 为弹出框设定内容
			**/
			setContent:function(html){
				var _this = this;
				_this.uiDialogContent.html(html);
			},
			/**
			**设定访问地址
			**/
			setFrameSrc:function(url){
				var _this = this;
				_this.uiDialogFrame.attr('src',url);
			},
			/**
			**开启拖动事件
			**/
			uiDialogStartDrag:function(){
				var _this = this;
				//$('#'+_this.uiDialogId+' .ui-dialog-title').mousedown
				
				_this.uiDialog.mousedown(function(e){
					//alert(e.clientY - this.offsetTop);
					//判断坐标是否在正确的坐标位置上
					if((e.clientY - this.offsetTop) <= _this.uiDialogTitle.height()+1){
						_this.settings.dragging = true;
						_this.settings.iX = e.clientX - this.offsetLeft;
						_this.settings.iY = e.clientY - this.offsetTop;
						this.setCapture && this.setCapture();
						return false;	
					}
				});
			},
			/**
			**开启移动事件
			**/
			uiDialogMoveDrag:function(){
				var _this = this;
				$(window.document).bind('mousemove.'+_this.uiDialogId,function(e){
					if(_this.settings.dragging){
						var e = e || window.event;
						var oX = e.clientX - _this.settings.iX;
						var oY = e.clientY - _this.settings.iY;
						var width = $("#"+_this.uiDialogId).width();
						var height = $("#"+_this.uiDialogId).height();
						//最左
						if(oX<=0)
							oX = 0;
						//最右
						if(oX+width+2>=$(window).width())
							oX = $(window).width() - 2 - width;
						//最上
						if(oY<=0)
							oY = 0;
						if(oY+height+2>=$(window).height())
							oY = $(window).height() - 2 - height;
						$("#"+_this.uiDialogId).css({"left":oX + "px", "top":oY + "px"});
						return false;
					}
				});
			},
			/**
			**结束拖动事件
			**/
			uiDialogMoveRelease:function(){
				var _this = this;
				$(window.document).bind('mouseup.'+_this.uiDialogId,function(e){
					_this.settings.dragging = false;
					//$("#"+_this.uiDialogId)[0].releaseCapture();
					e.cancelBubble = true;
				});
			}
		};
    }
})(jQuery);